﻿using System;

namespace VitCode.BaseDataPageCreator.Setting
{
	/// <summary>
	/// DateHelper 的摘要说明。
	/// </summary>
	public class DateHelper
	{
		public static DateTime GetYearDate(int year)
		{
			return new DateTime(year, 1, 1);
		}

		public static DateTime GetYearMonthDate(int year, int month)
		{
			if (year < 1900 || year > 9999 || month < 1 || month > 12)
				throw new ArgumentOutOfRangeException("日期", "输入的日期数值超出范围");
			return new DateTime(year, month, 1);
		}

		public static string GetYearMonthString(DateTime date)
		{
			return date.Year.ToString() + "-" + date.Month.ToString();
		}

		public static string GetYearMonthDayString(DateTime date)
		{
			return date.Year.ToString() + "-" + date.Month.ToString() + "-" + date.Day.ToString();
		}

		public static DateTime GetYearMonthDate(string dateString)
		{
			DateTime date;
			string[] ss = dateString.Split('-');
			if (ss.Length == 2)
			{
				int year = Convert.ToInt32(ss[0]);
				int month = Convert.ToInt32(ss[1]);
				date = GetYearMonthDate(year, month);
			}
			else
				throw new ArgumentOutOfRangeException("日期", "格式不正确");
			return date;
		}

		public static DateTime GetYearMonthDayDate(string dateString)
		{
			DateTime date;
			string[] ss = dateString.Split('-');
			if (ss.Length == 3)
			{
				int year = Convert.ToInt32(ss[0]);
				int month = Convert.ToInt32(ss[1]);
				int day = Convert.ToInt32(ss[2]);
				date = new DateTime(year, month, day);
			}
			else
				throw new ArgumentOutOfRangeException("日期", "格式不正确");
			return date;
		}

		public static string GetImportNumber()
		{
			return DateTime.Now.ToString("yyyyMMddHHmmssff");
		}

		/// <summary>
		/// 根据输入字符串转换成日期
		/// </summary>
		/// <param name="dateString"></param>
		/// <returns>返回日期，yyyymmddhhmm</returns>
		public static DateTime GetTimeAllFormat(string dateString)
		{
			string[] ss = dateString.Split(' ');
			DateTime date;				
			int year = 0,month = 0,day = 0,hour = 0,minute = 0;
			if (ss.Length == 2)
			{
				string[] ymd = ss[0].Split('-');
				string[] ymd2 = ss[0].Split('/');
				string[] hm = ss[1].Split(':');
				if (ymd.Length == 3)
				{
					year = Convert.ToInt32(ymd[0]);
					month = Convert.ToInt32(ymd[1]);
					day = Convert.ToInt32(ymd[2]);
				}
				if (ymd2.Length == 3)
				{
					year = Convert.ToInt32(ymd2[0]);
					month = Convert.ToInt32(ymd2[1]);
					day = Convert.ToInt32(ymd2[2]);
				}
				if (hm.Length > 0)
				{
					hour = Convert.ToInt32(hm[0]);
					minute = Convert.ToInt32(hm[1]);
				}
				date = new DateTime(year, month, day, hour, minute, 0);
			}
			else
				throw new ArgumentOutOfRangeException("日期", "格式不正确");
			return date;
		}

		/// <summary>
		/// 将日期时间格式转换成字符串
		/// </summary>
		/// <param name="dateTime"></param>
		/// <returns></returns>
		public static string GetTimeAllFormatString(DateTime dateTime)
		{
			return dateTime.Year.ToString() + "-" + dateTime.Month.ToString() + "-" + dateTime.Day.ToString() + " " + dateTime.Hour.ToString() + ":" + dateTime.Minute.ToString();
		}
	}
	
}
